// ====================================================================================
// MAIN

/* 
Params passed to this script:
object action was attached to
unit that activated action
index of action
*/

private ["_isAdmin","_player","_name","_index","_all_players"];
_player = _this select 0;			 
_name = _this select 1;	
_index = _this select 2;


if (player != _player) exitWith { };

if (CFG_AdminCmds == 1) then { 
	if (isnil "RMM_ui_punishment_init") then {
		RMM_ui_punishment_init = {
			[] spawn {
				private ["_add","_jailed","_all_players","_players","_banned"];
				_all_players = [];
				RMM_ui_punishment = [[],objNull];
				RMM_ui_punishment_banned = [[],""];
				RMM_ui_confirm_punishment = 0;
				// if (!serverCommandAvailable "#kick") then {
					// ctrlEnable [4, false];
				// };
				// if (!serverCommandAvailable "#kick") then {
					// ctrlEnable [5, false];
				// };
				if (IsMultiplayer) then {
					_all_players = playableUnits;
				} else {
					_all_players = [player];
				};
				_players = [];
				{
					_add = false;
					if (isplayer _x) then {
						if (!isnil "MSO_R_Member") then {
							_add = true;
							if (!((getPlayerUID _x) in MSO_R_Member)) then {
								_add = true;
							};
						} else {
							_add = true;
						};
					};
					if (_add) then {
						_jailed = _x getVariable "IS_PLAYER_JAILED";
						if (isnil "_jailed") then { _jailed = false; };
						if (_jailed) then {
							lbAdd [1101,Format["%1: %2 (%3)",localize "STR_DLG_JAILED", name _x, getPlayerUID _x]];
						} else {
							if (vehicle _x != _x) then {
								lbAdd [1101,Format["%1: %2 (%3) in %4 (%5)",str _x, name _x, getPlayerUID _x, typeof (vehicle _x),str(vehicle _x)]];
							} else {
								lbAdd [1101,Format["%1: %2 (%3)",str _x, name _x, getPlayerUID _x]];
							};
						};
						_players set [count _players, _x];
					};
				} foreach _all_players;
				if (!isnil "BTC_Logic") then {
					if (!isnil {BTC_logic getVariable "BTC_TKs_string"}) then {
						_banned = [];
						{
							lbAdd [1102,Format["%1 - %2",_x select 0, _x select 1]];
							_banned set[count _banned, (_x select 0)];
						} foreach (BTC_logic getVariable "BTC_TKs_string");
						RMM_ui_punishment_banned = [_banned,""];
					};
				};
				RMM_ui_punishment = [_players, objNull];
			};
		};
	};
	if (isnil "RMM_ui_punishment_jail") then {
		RMM_ui_punishment_jail = {
			if (debug_mso) then { diag_log format ["MSO-%1 RMM_ui_punishment_jail:%2",time,_this]; };
			if (!isnull (RMM_ui_punishment select 1)) then {
				if ((getPlayerUID (RMM_ui_punishment select 1)) in MSO_R_Member) exitWith { hint localize "STR_NOT_PUNISH_MEMBER"; };
				["punishment_jail",[(RMM_ui_punishment select 1),player]] call FNC_ADMINACTIONS_RAISE_EVENT;
				RMM_ui_punishment set [1,objNull];
			};
		};
	};
	if (isnil "RMM_ui_punishment_release") then {
		RMM_ui_punishment_release = {
			if (debug_mso) then { diag_log format ["MSO-%1 RMM_ui_punishment_release:%2",time,_this]; };
			if (!isnull (RMM_ui_punishment select 1)) then {
				if ((getPlayerUID (RMM_ui_punishment select 1)) in MSO_R_Member) exitWith { hint localize "STR_NOT_PUNISH_MEMBER"; };
				["punishment_unjail",[(RMM_ui_punishment select 1),player]] call FNC_ADMINACTIONS_RAISE_EVENT;
				RMM_ui_punishment set [1,objNull];
			};
		};
	};
	if (isnil "RMM_ui_punishment_kick") then {
		RMM_ui_punishment_kick = {
			if (debug_mso) then { diag_log format ["MSO-%1 RMM_ui_punishment_kick:%2",time,_this]; };
			if (!isnull (RMM_ui_punishment select 1)) then {
				if ((getPlayerUID (RMM_ui_punishment select 1)) in MSO_R_Member) exitWith { hint localize "STR_NOT_PUNISH_MEMBER"; };
				if (dialog) then { closedialog 0 };
				_dialog = createDialog "RMM_ui_confirm_punishment";
				waituntil {dialog};
				ctrlSetText [1, format["%1: %2 - %3",localize "STR_DLG_PUNISH_KICK",getPlayerUID(RMM_ui_punishment select 1),name(RMM_ui_punishment select 1)]];
				waituntil {(!dialog)};
				if (RMM_ui_confirm_punishment > 0) then {
					if (serverCommandAvailable "#kick") then {
						serverCommand format["#kick %1",getPlayerUID (RMM_ui_punishment select 1)];
						["punishment_kick",[(RMM_ui_punishment select 1),player]] call FNC_ADMINACTIONS_RAISE_EVENT;
					} else {
						["punishment_server",[(RMM_ui_punishment select 1),"kick",Format["kicked by %1 (%2)",name player, getPlayerUID player]]] call FNC_ADMINACTIONS_RAISE_EVENT;
					};
				};
				RMM_ui_punishment set [1,objNull];
			};
		};
	};
	if (isnil "RMM_ui_punishment_ban") then {
		RMM_ui_punishment_ban = {
			if (debug_mso) then { diag_log format ["MSO-%1 RMM_ui_punishment_ban:%2",time,_this]; };
			if (!isnull (RMM_ui_punishment select 1)) then {
				// if ((getPlayerUID (RMM_ui_punishment select 1)) in MSO_R_Member) exitWith { hint localize "STR_NOT_PUNISH_MEMBER"; };
				if (dialog) then { closedialog 0 };
				_dialog = createDialog "RMM_ui_confirm_punishment";
				waituntil {dialog};
				ctrlSetText [1, format["%1: %2 - %3",localize "STR_DLG_PUNISH_BAN",getPlayerUID(RMM_ui_punishment select 1),name(RMM_ui_punishment select 1)]];
				waituntil {(!dialog)};
				if (RMM_ui_confirm_punishment > 0) then {
					if (serverCommandAvailable "#kick") then {
						serverCommand format["#exec ban %1",getPlayerUID (RMM_ui_punishment select 1)];
						["punishment_ban",[(RMM_ui_punishment select 1),player]] call FNC_ADMINACTIONS_RAISE_EVENT;
					} else {
						["punishment_server",[(RMM_ui_punishment select 1),"ban",Format["banned by %1 (%2)",name player, getPlayerUID player]]] call FNC_ADMINACTIONS_RAISE_EVENT;
					};
				};
				RMM_ui_punishment set [1,objNull];
			};
		};
	};
	if (isnil "RMM_ui_punishment_unban") then {
		RMM_ui_punishment_unban = {
			if (debug_mso) then { diag_log format ["MSO-%1 RMM_ui_punishment_unban:%2",time,_this]; };
			private ["_tk_array","_uid","_i"];
			if ((RMM_ui_punishment_banned select 1) != "") then {
				_tk_array = (BTC_logic getVariable "BTC_TKs_string");
				for [{_i = 0},{_i < count _tk_array},{_i = _i + 1}] do {
					_uid = (_tk_array select _i) select 0;
					if (_uid == (RMM_ui_punishment_banned select 1)) then {
						_tk_array set [_i, "XxDeLxX"];
					};
				};
				_tk_array = _tk_array - ["XxDeLxX"];
				BTC_logic setVariable ["BTC_TKs_string",_tk_array,true];
				RMM_ui_punishment_banned set [1,""];
			};
		};
	};
	_isAdmin = serverCommandAvailable "#kick" || !isMultiplayer;
	
	if (_isAdmin || CFG_TeamAdmin || CFG_TeamModerator) then {
		_dialog = createDialog "RMM_ui_punishment";
	}else {
		hint localize "STR_CMD_NA";
	};	
};
// ====================================================================================